package main.com.oaer.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import main.com.oaer.dao.RoleDao;
import main.com.oaer.dao.UserRoleDao;
import main.com.oaer.model.UserRole;
import main.com.oaer.service.UserRoleService;

@Service
public class UserRoleServiceImpl implements  UserRoleService {

	@Autowired
	UserRoleDao userRoleDao;
	
	@Autowired
	RoleDao roleDao;
	
	@Override
	public void add(UserRole userRole) {
		UserRole tempUserRole = userRoleDao.findRoleByUserId(userRole.getUserId());
		userRoleDao.delete(tempUserRole);
		userRoleDao.add(userRole);
	}

	@Override
	public  List<Map<String,Object>> findAllRoles(Long  userId){
		 UserRole userRole = userRoleDao.findRoleByUserId(userId);
		 List<Map<String,Object>> roles = roleDao.findRolesMap();
		 if(userRole!=null){
			 for(Map<String,Object> role:roles){
				 if(role.get("roleId") == userRole.getRoleId()){
					 role.put("userId",userRole.getUserId());
				 }
			 }
		 }
		 return roles;
		 
	}

}
